/***************************************************************
 * Name:      EditorMain.cpp
 * Purpose:   Code for Application Frame
 * Author:     ()
 * Created:   2014-08-18
 * Copyright:  ()
 * License:
 **************************************************************/

#include "wx_pch.h"
#include "EditorMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(EditorFrame)
#include <wx/artprov.h>
#include <wx/bitmap.h>
#include <wx/icon.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(EditorFrame)
const long EditorFrame::ID_GLCANVAS1 = wxNewId();
const long EditorFrame::ID_GRID1 = wxNewId();
const long EditorFrame::ID_SPLITTERWINDOW1 = wxNewId();
const long EditorFrame::idMenuNew = wxNewId();
const long EditorFrame::idMenuOpen = wxNewId();
const long EditorFrame::idMenuSave = wxNewId();
const long EditorFrame::idMenuSaveAs = wxNewId();
const long EditorFrame::idMenuQuit = wxNewId();
const long EditorFrame::idMenuAbout = wxNewId();
const long EditorFrame::ID_STATUSBAR1 = wxNewId();
const long EditorFrame::idNewTool = wxNewId();
const long EditorFrame::idOpenTool = wxNewId();
const long EditorFrame::idSaveTool = wxNewId();
const long EditorFrame::idSaveAsTool = wxNewId();
const long EditorFrame::idUndoTool = wxNewId();
const long EditorFrame::idRedoTool = wxNewId();
const long EditorFrame::ID_TOOLBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(EditorFrame,wxFrame)
    //(*EventTable(EditorFrame)
    //*)
END_EVENT_TABLE()

EditorFrame::EditorFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(EditorFrame)
    wxMenuItem* aboutMenuItem;
    wxMenu* helpMenu;
    wxMenu* fileMenu;
    wxMenuItem* quitMenuItem;
    wxMenuBar* menuBar;

    Create(parent, id, _("Resource Editor"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE|wxSYSTEM_MENU|wxRESIZE_BORDER|wxSIMPLE_BORDER|wxFULL_REPAINT_ON_RESIZE, _T("id"));
    SetClientSize(wxSize(628,450));
    {
    wxIcon FrameIcon;
    FrameIcon.CopyFromBitmap(wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_NORMAL_FILE")),wxART_OTHER));
    SetIcon(FrameIcon);
    }
    SplitterWindow1 = new wxSplitterWindow(this, ID_SPLITTERWINDOW1, wxPoint(144,312), wxSize(808,450), wxSP_3D, _T("ID_SPLITTERWINDOW1"));
    SplitterWindow1->SetMinSize(wxSize(0,0));
    SplitterWindow1->SetMaxSize(wxSize(0,0));
    SplitterWindow1->SetMinimumPaneSize(10);
    SplitterWindow1->SetSashGravity(0.5);
    int GLCanvasAttributes_1[] = {
    WX_GL_RGBA,
    WX_GL_DOUBLEBUFFER,
    WX_GL_DEPTH_SIZE,      16,
    WX_GL_STENCIL_SIZE,    0,
    0, 0 };
    glCanvas = new wxGLCanvas(SplitterWindow1, ID_GLCANVAS1, wxPoint(159,205), wxDLG_UNIT(SplitterWindow1,wxSize(242,274)), 0, _T("ID_GLCANVAS1"), GLCanvasAttributes_1);
    Grid1 = new wxGrid(SplitterWindow1, ID_GRID1, wxPoint(413,2), wxSize(1,446), wxVSCROLL, _T("ID_GRID1"));
    Grid1->CreateGrid(10,2);
    Grid1->SetMinSize(wxSize(0,0));
    Grid1->SetMaxSize(wxDLG_UNIT(SplitterWindow1,wxSize(0,0)));
    Grid1->EnableEditing(true);
    Grid1->EnableGridLines(true);
    Grid1->SetColLabelSize(1);
    Grid1->SetRowLabelSize(16);
    Grid1->SetDefaultCellFont( Grid1->GetFont() );
    Grid1->SetDefaultCellTextColour( Grid1->GetForegroundColour() );
    SplitterWindow1->SplitVertically(glCanvas, Grid1);
    menuBar = new wxMenuBar();
    fileMenu = new wxMenu();
    newMenuItem = new wxMenuItem(fileMenu, idMenuNew, _("&New\tCtrl-N"), _("New file"), wxITEM_NORMAL);
    newMenuItem->SetBitmap(wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_NEW")),wxART_MENU));
    fileMenu->Append(newMenuItem);
    openMenuItem = new wxMenuItem(fileMenu, idMenuOpen, _("&Open\tCtrl-O"), _("Open document"), wxITEM_NORMAL);
    openMenuItem->SetBitmap(wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_FILE_OPEN")),wxART_MENU));
    fileMenu->Append(openMenuItem);
    saveMenuItem = new wxMenuItem(fileMenu, idMenuSave, _("&Save\tCtrl-S"), _("Save document"), wxITEM_NORMAL);
    saveMenuItem->SetBitmap(wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_FILE_SAVE")),wxART_MENU));
    fileMenu->Append(saveMenuItem);
    MenuItem1 = new wxMenuItem(fileMenu, idMenuSaveAs, _("Save As"), _("Save document as"), wxITEM_NORMAL);
    fileMenu->Append(MenuItem1);
    fileMenu->AppendSeparator();
    quitMenuItem = new wxMenuItem(fileMenu, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    quitMenuItem->SetBitmap(wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_QUIT")),wxART_MENU));
    fileMenu->Append(quitMenuItem);
    menuBar->Append(fileMenu, _("&File"));
    helpMenu = new wxMenu();
    aboutMenuItem = new wxMenuItem(helpMenu, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    helpMenu->Append(aboutMenuItem);
    menuBar->Append(helpMenu, _("Help"));
    SetMenuBar(menuBar);
    statusBar = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    statusBar->SetFieldsCount(1,__wxStatusBarWidths_1);
    statusBar->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(statusBar);
    toolBar = new wxToolBar(this, ID_TOOLBAR1, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL|wxNO_BORDER, _T("ID_TOOLBAR1"));
    newToolBarItem = toolBar->AddTool(idNewTool, _("New"), wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_NEW")),wxART_TOOLBAR), wxNullBitmap, wxITEM_NORMAL, _("New file"), wxEmptyString);
    ToolBarItem1 = toolBar->AddTool(idOpenTool, _("Open"), wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_FILE_OPEN")),wxART_TOOLBAR), wxNullBitmap, wxITEM_NORMAL, _("Open"), wxEmptyString);
    ToolBarItem2 = toolBar->AddTool(idSaveTool, _("Save"), wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_FILE_SAVE")),wxART_TOOLBAR), wxNullBitmap, wxITEM_NORMAL, _("Save"), wxEmptyString);
    ToolBarItem3 = toolBar->AddTool(idSaveAsTool, _("Save As"), wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_FILE_SAVE_AS")),wxART_TOOLBAR), wxNullBitmap, wxITEM_NORMAL, _("Save As"), wxEmptyString);
    toolBar->AddSeparator();
    ToolBarItem4 = toolBar->AddTool(idUndoTool, _("Undo"), wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_UNDO")),wxART_TOOLBAR), wxNullBitmap, wxITEM_NORMAL, _("Undo"), wxEmptyString);
    ToolBarItem5 = toolBar->AddTool(idRedoTool, _("Redo"), wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_REDO")),wxART_TOOLBAR), wxNullBitmap, wxITEM_NORMAL, _("Redo"), wxEmptyString);
    toolBar->AddSeparator();
    toolBar->Realize();
    SetToolBar(toolBar);
    fileOpenDialog = new wxFileDialog(this, _("Select file"), wxEmptyString, wxEmptyString, wxFileSelectorDefaultWildcardStr, wxFD_OPEN|wxFD_FILE_MUST_EXIST, wxDefaultPosition, wxDefaultSize, _T("wxFileDialog"));
    fileSaveDialog = new wxFileDialog(this, _("Select file"), wxEmptyString, wxEmptyString, wxFileSelectorDefaultWildcardStr, wxFD_SAVE, wxDefaultPosition, wxDefaultSize, _T("wxFileDialog"));

    Connect(ID_GRID1,wxEVT_GRID_CELL_LEFT_CLICK,(wxObjectEventFunction)&EditorFrame::OnGrid1CellLeftClick);
    Connect(idMenuNew,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&EditorFrame::OnNew);
    Connect(idMenuOpen,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&EditorFrame::OnOpen);
    Connect(idMenuSave,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&EditorFrame::OnSave);
    Connect(idMenuSaveAs,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&EditorFrame::OnSaveAs);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&EditorFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&EditorFrame::OnAbout);
    Connect(idNewTool,wxEVT_COMMAND_TOOL_CLICKED,(wxObjectEventFunction)&EditorFrame::OnNew);
    Connect(idOpenTool,wxEVT_COMMAND_TOOL_CLICKED,(wxObjectEventFunction)&EditorFrame::OnOpen);
    Connect(idSaveTool,wxEVT_COMMAND_TOOL_CLICKED,(wxObjectEventFunction)&EditorFrame::OnSave);
    Connect(idSaveAsTool,wxEVT_COMMAND_TOOL_CLICKED,(wxObjectEventFunction)&EditorFrame::OnSaveAs);
    Connect(wxID_ANY,wxEVT_CLOSE_WINDOW,(wxObjectEventFunction)&EditorFrame::OnClose);
    //*)
}

EditorFrame::~EditorFrame()
{
    //(*Destroy(EditorFrame)
    //*)
}

void EditorFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void EditorFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void EditorFrame::OnClose(wxCloseEvent& event)
{
	wxExit();
}

void EditorFrame::OnOpen(wxCommandEvent& event)
{
	if (fileOpenDialog->ShowModal() == wxID_OK){
		wxString path = fileOpenDialog->GetPath();
		wxString str;
		str.Printf(wxT("File Open: %s"), path);
		wxPuts(str);
	}
}

void EditorFrame::OnSave(wxCommandEvent& event)
{
	if (fileSaveDialog->ShowModal() == wxID_OK){
		wxString path = fileSaveDialog->GetPath();
		wxString str;
		str.Printf(wxT("File Save: %s"), path);
		wxPuts(str);
	}
}

void EditorFrame::OnSaveAs(wxCommandEvent& event)
{
	if (fileSaveDialog->ShowModal() == wxID_OK){
		wxString path = fileSaveDialog->GetPath();
		wxString str;
		str.Printf(wxT("File Save As: %s"), path);
		wxPuts(str);
	}
}

void EditorFrame::OnNew(wxCommandEvent& event)
{
	wxPuts(wxT("New file pressed"));
}

void EditorFrame::OnGrid1CellLeftClick(wxGridEvent& event)
{
}
